﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jzy.BLL.Contact;

using Jzy.Model.Contact;
using Frame.Lib.Utilities;
using Frame.Lib.Common;
using Frame.Lib.Core.Tree.Dir;
using Frame.Lib.Core.Right;
using Frame.Lib.Enums;


public partial class Contact_Outer_CardList : PageBase
{   
    private int DirID
    {
        get
        {
            return int.Parse(StrUtil.DecryptUrl(Request["DirID"]));
        }
    }

    private ListMode ContactListMode
    {
        get
        {
            if (AppSession.Session["ContactListMode"] == null)
            {
                AppSession.Session["ContactListMode"] = ListMode.Card;
            }
            return (ListMode)AppSession.Session["ContactListMode"];
        }
        set
        {
            AppSession.Session["ContactListMode"] = value;
        }
    }
    private ContactBO _contact = new ContactBO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblDirName.Text = (new DirBO("ContactDir")).GetName(DirID);
            BindList();
        }
        AddAttr();
        JSUtil.SetTableHeight(content.ClientID, 60);
    }

    private void AddAttr()
    {
        JSUtil.ClickBtn(lnkDispMode, btnDispMode);
        JSUtil.ClickBtn(lnkExport, btnExport);
        JSUtil.ShowForm(lnkAdd, "增加联系人", null, GlobalVar.WebCurrPath + "Edit.aspx?DirID=" + DirID, 500, 350);

        SystemRightBO right = new SystemRightBO(RightOwnerType.User);
        if (!right.HaveRight(AppSession.UserID, new SystemRight( SystemRightType.联系人, SystemRightName.可增加)))
        {
            liAdd.Visible = false;
        }
    }

    private void BindList()
    {       
        DataTable dt = _contact.GetList(DirID, AppSession.UserID);
        DataView dv = dt.DefaultView;

        foreach (DataRow dr in dt.Rows)
        {
            if (dr["Headship"].ToString() != "")
            {
                dr["Headship"] = "<b>[</b>" + dr["Headship"] + "<b>]</b>";
            }
        }

        if (txtCondition.Text.Trim() != "")
        {
            dv.RowFilter = "Name like '%" + txtCondition.Text.Trim() + "%'";
        }

        lv.DataKeyNames = new string[] { "ID" };
        lv.DataSource = dv;
        lv.DataBind();
        if (dv.Count == 0)
        {
            content.Style["margin-right"] = "0px";
        }
        lblCount.Text = "[ 共 " + dv.Count + " 个联系人 ]&nbsp;";
    }
    protected void btnDispMode_Click(object sender, EventArgs e)
    {
        ContactListMode = ListMode.List;
        Response.Redirect("List.aspx?DirID=" + StrUtil.EncryptUrl(DirID), true);
    }
   
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindList();
    }
    
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindList();
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        BindList();
        this.EnableViewState = false;
        _contact.ExportExcel(lv.DataSource);
    }
    protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            string id = lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString();
            HyperLink lnkName = (HyperLink)e.Item.FindControl("lnkName");
            JSUtil.ShowForm(lnkName, "联系人信息", null, "Edit.aspx?ID=" + StrUtil.EncryptUrl(id), 500, 350);
        }
    }
}
